package com.cqw;


import com.cqw.commontest.stream.User;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-11-15 10:44
 * @Description:
 */
public class CommonTest {


    public static void main(String[] args) throws Exception {
        Map<String, String> data = new HashMap<>();
        data.put("body", "CharlotteTilbury官方商城");
        data.put("attach", "ct");
        data.put("out_trade_no", "XCXCT2206210000001057");
        data.put("total_fee", "2");
        data.put("spbill_create_ip","8.8.8.8");
        Date now = new Date();
        data.put("time_start", "20220621184848");
        data.put("time_expire", "20220621205848");

        data.put("trade_type", "JSAPI");
        data.put("openid", "oIcgO5h9a_uYWkv6CIPZppLsE92Q");
        data.put("notify_url","http://106.14.30.60:16018/api/sp-pay/response/ct");

        data.put("appid", "wxea57740ecc23ad73");
        data.put("mch_id","1627473462");
        data.put("nonce_str", "wMGCCNh8K053MkonqSqo861gT1ANbQEY");


        String charlottetilburyCharlottetilbury = generateSignature(data, "CharlottetilburyCharlottetilbury");
        System.out.println(charlottetilburyCharlottetilbury);



        String str = "pages/product/detail/index?id={productId}";
        String replace = str.replace("{productId}", "111");
        String replace1 = replace.replace("{skuId}", "222");
        System.out.println(replace1);
    }

    public static String generateSignature(final Map<String, String> data, String key) throws Exception {
        Set<String> keySet = data.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            if (k.equals("sign")) {
                continue;
            }
            if (data.get(k).trim().length() > 0) // 参数值为空，则不参与签名
                sb.append(k).append("=").append(data.get(k).trim()).append("&");
        }
        sb.append("key=").append(key);
        System.out.println(sb.toString());
        return MD5(sb.toString()).toUpperCase();

    }

    public static String MD5(String data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100), 1, 3);
        }
        return sb.toString().toUpperCase();
    }

}
